/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson6;

import java.io.Serializable;
import java.util.Iterator;

/**
 * Класс вектор
 *
 * @author netcracker
 */
public class ArrayVector implements IVector, Cloneable, Serializable {

    private double[] array;

    public ArrayVector(double[] array) {
        this.array = array;
    }

    public ArrayVector(int n) {
        this.array = new double[n];
    }

    public ArrayVector(ArrayVector myVector) {
        this.array = new double[myVector.getLength()];
        for (int i = 0; i < myVector.getLength(); i++) {
            array[i] = myVector.get(i);
        }
    }

    @Override
    public double get(int id) {
        return this.array[id];
    }

    @Override
    public void set(double value, int position) {
        this.array[position] = value;
    }

    @Override
    public void fill(double[] mass) {
        System.arraycopy(mass, 0, this.array, 0, mass.length);
    }

    @Override
    public void fill(IVector myVector) {
        if (this.array.length == myVector.getLength()) {
            for (int i = 0; i < myVector.getLength(); i++) {
                this.array[i] = myVector.get(i);
            }
        }
    }
@Override
    public boolean isSame(IVector myVector) {
        if (this.array.length == myVector.getLength()) {
            for (int i = 0; i < array.length; i++) {
                boolean b = this.array[i] == myVector.get(i);
                if (b == false) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int getLength() {
        return this.array.length;
    }

    @Override
    public void multiply(double f) {
        for (int i = 0; i < this.array.length; i++) {
            this.array[i] = this.array[i] * f;
        }
    }

    @Override
    public void plus(IVector myVector) {
        if (this.array.length == myVector.getLength()) {
            for (int i = 0; i < this.array.length; i++) {
                this.array[i] = this.array[i] + myVector.get(i);
            }
        }
    }

    @Override
    public void add(double o) {
        double[] newarray = new double[this.getLength() + 1];
        System.arraycopy(this.array, 0, newarray, 0, this.getLength());
        newarray[this.array.length] = o;
        this.array = newarray;
    }

    @Override
    public void delete(int index) {
        double[] newarray = new double[this.getLength() - 1];
        System.arraycopy(this.array, 0, newarray, 0, index);
        System.arraycopy(this.array, index + 1, newarray, index, this.getLength() - index - 1);
        this.array = newarray;
    }

    @Override
    public void sort() {
        for (int i = this.getLength() - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (this.array[j] > this.array[j + 1]) {
                    this.swap(j, j + 1);
                }
            }
        }
    }

    @Override
    public void swap(int i, int j) {
        double t = this.array[i];
        this.array[i] = this.array[j];
        this.array[j] = t;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer(this.getClass().toString());
        stringBuffer.append("\n" + "Contains:" + "\n");
        for (int i = 0; i < this.getLength(); i++) {
            stringBuffer.append(this.get(i)).append("\n");
        }
        return stringBuffer.toString();
    }

    @Override
    public int hashCode() {
        long result = Double.doubleToLongBits(this.array[0]);

        int ups;
        int downs;
        for (int i = 1; i < this.getLength() - 1; i++) {
            result = result ^ Double.doubleToLongBits(this.get(i));

        }
        downs = (int) result;
        result = result >> 32;
        ups = (int) result;
        return downs ^ ups;
    }

    @Override
    public boolean equals(Object obj) {
        obj = (Vectors) obj;
        if (obj == null) {
            return false;
        }

        if (!(obj instanceof IVector)) {
            return false;
        }
        final IVector other = (IVector) obj;

        if (this.getLength() != other.getLength()) {
            return false;
        }
        if (this.hashCode() != obj.hashCode()) {
            return false;
        }

        if (!this.isSame(other)) {
            return false;
        }

        return true;
    }

    @Override
    public ArrayVector clone() throws CloneNotSupportedException {
        ArrayVector result;
        result = (ArrayVector) super.clone();
        result.array = array.clone();
        return result;
    }

    @Override
    public Iterator<Double> iterator() {
        return new Iterator<Double>() {
            int marker = 0;

            @Override
            public boolean hasNext() {
                if (array.length > marker) {
                    return true;
                }
                return false;
            }

            @Override
            public Double next() {
                double result = get(marker);
                marker++;

                return result;

            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
    }
}
